package com.paper.tiger.easy.rpc.s.client.server;

import com.paper.tiger.easy.rpc.s.client.common.LetterType;
import com.paper.tiger.easy.rpc.s.client.common.Dove;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.ReferenceCountUtil;

/**
 *  心跳 handler 维持心跳,客户端发送心跳包过来,响应心跳包
 */
@ChannelHandler.Sharable
public class InvokeServerHeartBeatHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

        Dove data = (Dove) msg;
        // 收到服务器心跳
        if (data.getLetterType().equals(LetterType.HEART_BEAT.getCode())){
            ReferenceCountUtil.release(msg);
        }else {
            // 如果不是心跳包,向下个handler 传递
            ctx.fireChannelRead(msg);
        }

    }


}
